import dayjs, { Dayjs } from 'dayjs'
import { DATE_FORMAT, TIME_FORMAT } from '@/constants'

export const isFalsy = (value: unknown) => (value === 0 ? false : !value)

export const isVoid = (value: unknown) => value === undefined || value === null || value === ''

export const isEmptyObject = (value: any) => !value || Object.keys(value).length === 0

export const mySimpleTypeof = (data: unknown) => {
  if (data === null) return 'null'
  return typeof data
}

export const myTypeof = (data: unknown) => {
  return Object.prototype.toString
    .call(data)
    .replace(/\[object\s(.+)\]/, '$1')
    .toLowerCase()
}

export const isEqualObject = (o1, o2) => {
  return JSON.stringify(o1) === JSON.stringify(o2)
}

export const cleanObject = (object?: { [key: string]: unknown }) => {
  if (!object) {
    return {}
  }
  const result = { ...object }
  Object.keys(result).forEach(key => {
    const value = result[key]
    if (isVoid(value)) {
      delete result[key]
    }
  })
  return result
}

/**
 * 传入一个对象，和键集合，返回对应的对象中的键值对
 * @param obj
 * @param keys
 */
export const subset = <
  O extends { [key in string]: unknown },
  K extends keyof O
>(
  obj: O,
  keys: K[]
) => {
  const filteredEntries = Object.entries(obj).filter(([key]) =>
    keys.includes(key as K)
  );
  return Object.fromEntries(filteredEntries) as Pick<O, K>;
};

export const formatRequestParams = (object: any, timeFormat = TIME_FORMAT):any => {
  if (!object) {
    return {}
  }
  const result = { ...object }
  Object.keys(result).forEach(key => {
    const value = result[key]
    if (isVoid(value)) {
      delete result[key]
    } else if (/time/.test(key)) {
      // 处理时间
      if (/time/.test(key)) {
        let timePrefix = ''
        if (key.includes('time')) {
          timePrefix = `${key.split('time')[0]}`
        }
        result[`${timePrefix}start_time`] = dayjs((result[key] as Dayjs[])[0]).format(timeFormat)
        result[`${timePrefix}end_time`] = dayjs((result[key] as Dayjs[])[1]).format(timeFormat)
        delete result[key]
      }
    } else if (Array.isArray(value)) {
      if (!value.includes('all') && value.length > 0) {
        result[key] = JSON.stringify(value.map(item => (item === '未定义' ? '' : item)))
      } else {
        delete result[key]
      }
    } else {
    }
  })
  return result
}

export const formatParamsToURL = (object?: { [key: string]: unknown }) => {
  if (!object) {
    return {}
  }
  const result = { ...object }
  Object.keys(result).forEach(key => {
    const value = result[key]
    if (isVoid(value)) {
      delete result[key]
    } else {
      // 处理时间
      if (/created_at|updated_at/.test(key)) {
        result[key] = dayjs(result[key] as Dayjs).format(DATE_FORMAT)
      }
      if (/time/.test(key)) {
        result[key] = `${dayjs((result[key] as Dayjs[])[0]).format(DATE_FORMAT)},${dayjs(
          (result[key] as Dayjs[])[1]
        ).format(DATE_FORMAT)}`
      }
    }
  })
  return result
}

export const formatParamsFromURL = (object: { [key: string]: unknown }) => {
  if (!object) {
    return {}
  }
  const result = { ...object }
  Object.keys(result).forEach(key => {
    const value = result[key]
    if (isVoid(value)) {
      delete result[key]
    } else {
      // 处理时间
      if (/created_at|updated_at/.test(key)) {
        result[key] = dayjs(value!)
      }
      if (/time/.test(key)) {
        const times = (value as string).split(',')
        result[key] = [dayjs(times[0]), dayjs(times[1])]
      }
    }
  })
  return result
}
